//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using CommunityServer.Components;
using CommunityServer.Configuration;

namespace CommunityServer.Files.Components
{
	/// <summary>
	/// Summary description for GalleryUrls.
	/// </summary>
	public class FileGalleryUrls : ApplicationUrls
	{

		public static readonly string FileGalleryUrlsProviderName = "FileGalleryUrlsProvider";

		public FileGalleryUrls() { }

		#region Instance

		private static FileGalleryUrls _defaultInstance = null;

		static FileGalleryUrls()
		{
			CreateDefaultCommonProvider();
		}

		/// <summary>
		/// Returns an instance of the user-specified data provider class.
		/// </summary>
		/// <returns>An instance of the user-specified data provider class.  This class must inherit the
		/// CommonDataProvider interface.</returns>
		public static FileGalleryUrls Instance() 
		{
			return _defaultInstance;
		}

		public static FileGalleryUrls Instance (Provider dataProvider) 
		{
			FileGalleryUrls fdp = CSCache.Get(dataProvider.Name) as FileGalleryUrls;
			if(fdp == null)
			{
				fdp = DataProviders.Invoke(dataProvider) as FileGalleryUrls;
				CSCache.Max(dataProvider.Name,fdp);
			}
			return fdp;
		}

		/// <summary>
		/// Creates the Default CommonDataProvider
		/// </summary>
		private static void CreateDefaultCommonProvider()
		{
			// Read the configuration specific information
			// for this provider
			//
			Provider urlProvider = (Provider) CSConfiguration.GetConfig().Providers[FileGalleryUrlsProviderName];

			// Create the instance for the provider, or fall back on the builtin one
			//
			if(urlProvider != null)
				_defaultInstance = CreateInstance(urlProvider) as FileGalleryUrls;
			else
				_defaultInstance = new FileGalleryUrls();
		}
        
		#endregion

		#region File Server Urls

		#region Public Urls

		public virtual string Home
		{
			get { return FormatUrl("fileshome"); }
		}

		public virtual string UploadFile(int sectionID)
		{
			return FormatUrl("files_UploadFile", sectionID);
		}

		public virtual string ViewFolderGroup(int groupID)
		{
			return FormatUrl("files_ViewFolderGroup", groupID);
		}

		public virtual string ViewFolder(string applicationKey)
		{
			return FormatUrl("files_ViewFolder", applicationKey);
		}

		public virtual string ViewCategory(string applicationKey, int categoryID)
		{
			return FormatUrl("files_ViewCategory", applicationKey, categoryID);
		}

		public virtual string ViewEntry(string applicationKey, int entryID)
		{
			return FormatUrl("files_ViewEntry", applicationKey, entryID);
		}

		public virtual string Search(string query, string usersList)
		{
			return FormatUrl("files_Search", query, usersList);
		}

		public virtual string Download(Entry e)
		{
			return Download(e.PostID);
		}
		
		public virtual string Download(int PostID)
		{
			return FormatUrl("files_Download", PostID);
		}

		public virtual string TagsHome(string applicationKey)
		{
			return FormatUrl("files_ViewFolder_AllTags", applicationKey);
		}

		public virtual string TagsBrowser(string applicationKey, string[] tagNames)
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			foreach (string tag in tagNames)
			{
				if (sb.Length > 0)
					sb.Append("/");

				sb.Append(Globals.UrlEncodePathComponent(tag));
			}

			return FormatUrl("files_ViewFolder_Tags", applicationKey, sb.ToString());
		}

		public virtual string Rss(string applicationKey)
		{
			return FormatUrl("files_Rss", applicationKey);
		}

		public virtual string Rss(string applicationKey, string[] tagNames)
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			foreach (string tag in tagNames)
			{
				if (sb.Length > 0)
					sb.Append("/");

				sb.Append(Globals.UrlEncodePathComponent(tag));
			}
			return FormatUrl("files_Rss_Tags", applicationKey, sb.ToString() );
		}

		public virtual string Rss(string applicationKey, string[] tagNames, bool andTags)
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			foreach (string tag in tagNames)
			{
				if (sb.Length > 0)
					sb.Append("/");

				sb.Append(Globals.UrlEncodePathComponent(tag));
			}
			return FormatUrl("files_Rss_TagsAnd", applicationKey, sb.ToString(), Convert.ToInt32(andTags) );
		}

		public virtual string EntryAdmin(string applicationKey, int pictureID)
		{
			return FormatUrl("files_EntryAdmin", applicationKey, -1, pictureID);
		}

		public virtual string DeleteEntry(string applicationKey, int entryID)
		{
			return FormatUrl("files_DeleteEntry", applicationKey, -1, entryID);
		}

		#endregion

		#region Admin Urls

		public virtual string Admin_AdminFolder(string applicationKey)
		{
			return FormatUrl("files_Admin_AdminFolder", applicationKey);
		}

		public virtual string Admin_AdminFolder_ReturnUrl(string applicationKey, string url, string args)
		{
			return FormatUrl("files_Admin_AdminFolder_ReturnUrl", applicationKey, url, args);
		}

		public virtual string Admin_AdminFolder(int sectionID)
		{
			return FormatUrl("files_Admin_AdminFolder2", sectionID);
		}

		public virtual string Admin_AdminFolder_ReturnUrl(int sectionID, string url, string args)
		{
			return FormatUrl("files_Admin_AdminFolder2_ReturnUrl", sectionID, url, args);
		}

		public virtual string Admin_ManageCategories(string applicationKey)
		{
			return FormatUrl("files_Admin_ManageCategories", applicationKey);
		}

		public virtual string Admin_AdminCategory(string applicationKey, int categoryID)
		{
			return FormatUrl("files_Admin_AdminCategory", applicationKey, categoryID);
		}

		public virtual string Admin_AdminCategory_ReturnUrl(string applicationKey, int categoryID, string url, string args)
		{
			return FormatUrl("files_Admin_AdminCategory_ReturnUrl", applicationKey, categoryID, url, args);
		}

		public virtual string Admin_ManageEntries(string applicationKey)
		{
			return FormatUrl("files_Admin_ManageEntries", applicationKey);
		}

		public virtual string Admin_AdminEntry(string applicationKey, int categoryID, int pictureID)
		{
			return FormatUrl("files_Admin_AdminEntry", applicationKey, categoryID, pictureID);
		}

		public virtual string Admin_AdminEntry_ReturnUrl(string applicationKey, int categoryID, int pictureID, string url, string args)
		{
			return FormatUrl("files_Admin_AdminEntry_ReturnUrl", applicationKey, categoryID, pictureID, url, args);
		}

		#endregion

		#region System Admin Urls

		public virtual string Admin_ManageFolders
		{
			get { return FormatUrl("files_Admin_ManageFolders"); }
		}

		public virtual string Admin_GroupAdmin(int forumGroupID)
		{
			return FormatUrl("files_Admin_GroupAdmin", forumGroupID);
		}

		public virtual string Admin_ManagePermissions
		{
			get {  return FormatUrl("files_Admin_ManagePermissions"); }
		}

		public virtual string Admin_AdminPermission(int galleryID, Guid roleID)
		{
			return FormatUrl("files_Admin_AdminPermission", galleryID, roleID);
		}

		public virtual string Admin_AdminPermission_ReturnUrl(int galleryID, Guid roleID, string url, string args)
		{
			return FormatUrl("files_Admin_AdminPermission_ReturnUrl", galleryID, roleID, url, args);
		}

		public virtual string Admin_ManageDefaultPermissions
		{
			get {  return FormatUrl("files_Admin_ManageDefaultPermissions"); }
		}

		public virtual string Admin_AdminDefaultPermission(Guid roleID)
		{
			return FormatUrl("files_Admin_AdminDefaultPermission", roleID);
		}

		public virtual string Admin_AdminDefaultPermission_ReturnUrl(Guid roleID, string url, string args)
		{
			return FormatUrl("files_Admin_AdminDefaultPermission_ReturnUrl", roleID, url, args);
		}

		#endregion

		#region Moderation

		public virtual string ModerationHome
		{
			get { return FormatUrl("files_Moderation_Home"); }
		}

		public virtual string ModerateFolder(int sectionID)
		{
			return FormatUrl("files_Moderation_Folder", sectionID);
		}

		#endregion

		#endregion

	}
}